import matplotlib.pyplot as plt
import pandas as pd

import os

from pyecharts import options as opt
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
from pyecharts.charts import Line
from pyecharts.charts import Pie

plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置字体为SimHei
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

def display_big_market_value_stocks2(stocks_info_df : pd.DataFrame):
    plt.figure(figsize=(10, 15))
    #plt.barh(stocks_info_df["名称"], stocks_info_df["总市值"])
    plt.bar(stocks_info_df["名称"], stocks_info_df["总市值"])
    plt.title("市值千亿以上的股票")
    plt.ylabel("股票名称")
    plt.xlabel("市值")
    plt.xticks(rotation=90)
    plt.show()

def display_market_level(dirname:str, stocks_list : list):
    
    lables_list = ["五千亿", "一千亿", "五百亿", "一百亿", "五十亿", "五十亿以下", "停牌股"]
    counts_list = []
    pre_count = 0
    index = 0
    for item in stocks_list:
        count = len(item)
        if index == 6:
            counts_list.append(count)
        else:
            counts_list.append(count - pre_count)
        pre_count = count
        index += 1

    print(counts_list)
    # 创建饼图对象
    pie_obj = Pie()

    # 添加数据
    pie_obj.add("", [list(z) for z in zip(lables_list, counts_list)])

    # 设置全局配置项  
    pie_obj.set_global_opts(title_opts=opt.TitleOpts(title="股票市值分布图"))

    pie_obj.render(dirname + "股票市值分布图.html")
    os.system(dirname + "股票市值分布图.html")

def display_big_market_value_stocks(dirname:str, stocks_info_df : pd.DataFrame):
    name_list = stocks_info_df["名称"].to_list()
    market_value_list = stocks_info_df["总市值"].to_list()
    barobj = (
        Bar(init_opts=opt.InitOpts(theme=ThemeType.LIGHT, width="100%", height="600px"))
        .add_xaxis(name_list)
        .add_yaxis("总市值", market_value_list)
        .set_global_opts(xaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(rotate=-5)),
                         title_opts=opt.TitleOpts(title="市值千亿以上的股票"),
                         brush_opts=opt.BrushOpts(),
                         datazoom_opts=opt.DataZoomOpts(),
                         yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value} 千亿")))
    )

    barobj.render(dirname +"权重股.html")
    os.system(dirname + "权重股.html")

def display_price_change(dirname:str, stocks_info_df : pd.DataFrame):
    stocks_info_df =  stocks_info_df.sort_values(by="年初至今涨跌幅", ascending=False)
    name_list = stocks_info_df["名称"].to_list()
    barobj = (
        Bar(init_opts=opt.InitOpts(theme=ThemeType.LIGHT, width="100%", height="600px"))
        .add_xaxis(name_list)
        .add_yaxis("60日涨跌幅", stocks_info_df["60日涨跌幅"].to_list())
        .add_yaxis("年初至今涨跌幅", stocks_info_df["年初至今涨跌幅"].to_list())
        .set_global_opts(xaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(rotate=-5)),
                         title_opts=opt.TitleOpts(title="股票涨跌幅统计"),
                         brush_opts=opt.BrushOpts(),
                         datazoom_opts=opt.DataZoomOpts(type_="inside"),
                         yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value} %")))
    )
    barobj.render(dirname + "stock_price_change.html")
    os.system(dirname + "stock_price_change.html")

def display_price_change(dirname:str, stocks_info_df : pd.DataFrame, period:str):
    stocks_info_df =  stocks_info_df.sort_values(by=period, ascending=False)
    name_list = stocks_info_df["名称"].to_list()
    
    # 初始化参数
    bar_init_opts = opt.InitOpts(theme=ThemeType.LIGHT, width="100%", height="600px") 
    # 创建水平条形图对象
    bar_obj = Bar(init_opts=bar_init_opts)

    bar_obj.add_xaxis(name_list)
    bar_obj.add_yaxis(period, stocks_info_df[period].to_list())
    bar_obj.set_global_opts(xaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(rotate=-5)),
                         title_opts=opt.TitleOpts(title=period),
                         brush_opts=opt.BrushOpts(),
                         datazoom_opts=opt.DataZoomOpts(type_="inside"),
                         yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value} %")))
    
    bar_obj.render(dirname + period + "stock_price_change.html")
    os.system(dirname + period + "stock_price_change.html")

def display_deal_amount(dirname:str, deal_volume_df : pd.DataFrame):
   line = (
            Line(init_opts=opt.InitOpts(width="100%", height="600px"))
            .add_xaxis(deal_volume_df["交易日"].to_list())
            .add_yaxis("上交所成交额", deal_volume_df["上交所成交额"].to_list(), label_opts=opt.LabelOpts(is_show=False))# 设置为 False 以隐藏数据标签
            .add_yaxis("深交所成交额", deal_volume_df["深交所成交额"].to_list(), label_opts=opt.LabelOpts(is_show=False))# 设置为 False 以隐藏数据标签
            .add_yaxis("两市总成交额", deal_volume_df["两市总成交额"].to_list(), label_opts=opt.LabelOpts(is_show=False))
            .set_global_opts(title_opts=opt.TitleOpts(title="成交趋势图"),
                             yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value} 亿元")))  # 在这里添加单位，例如 "单位" 可以是 "℃", "元" 等  )
            )
   line.render(dirname + "deal_data.html")
   os.system(dirname + "deal_data.html")